<%@page language="java" contentType="text/html; charset=UTF-8"%>
<%@page import="java.util.*,com.qunar.common.admin.util.ResourceNode" %>
<%@ taglib prefix="web" uri="http://meerkat.qunar.com/web"%>
<%!
    String loopTree(ResourceNode treeNode, int deep, int domId) {
        StringBuilder sb = new StringBuilder();
        if(treeNode != null) {
            sb.append(buildNodeStr(treeNode, deep*20));
            for(ResourceNode node: treeNode.getChildren()) {
                if(node.getChildren() != null && node.getChildren().size() > 0) {
                    sb.append(loopTree(node, deep+1, domId));
                } else {
                    sb.append(buildNodeStr(node, (deep+1)*20));
                }
            }
        }
        return sb.toString();
    }
    
    String buildNodeStr(ResourceNode node, int marginLeft) {
        StringBuilder sb = new StringBuilder();
        sb.append("<tr>");
        sb.append("<td class=\"res_add\" style=\"padding-left:"+ marginLeft +"px;\">");
        sb.append(String.format("<input type=\"checkbox\" id=\"node_%s\" name=\"node\" value=\"%s\" onclick=\"selChildren(this, '%s');\" />&nbsp;&nbsp;", node.getId(), node.getId(), getChildrenId(node)));
        sb.append(node.getName());
        sb.append("</td>");
        sb.append("</tr>\r\n");
        return sb.toString();
    }
    
    String getChildrenId(ResourceNode treeNode) {
		StringBuilder sb = new StringBuilder();
		List<ResourceNode> children = treeNode.getChildren();
		for(ResourceNode node : children) {
		    sb.append(node.getId()).append(",");
		    if(node.getChildren().size() > 0) {
			sb.append(getChildrenId(node));
		    }
		}
		return sb.toString();
    }
%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<web:designCss/>
<%@include file="/WEB-INF/include/head.jsp" %>
<script src="${PATH}resources/js/fw/app/qadmin.app.resource.js"></script>
</head>
<body>
    <div class="wrapper">
        <web:titleBar title="用户组权限" />

        <div class="main content">
        <table class="comm_tree">
            <%
                List<ResourceNode> nodeList = (List<ResourceNode>) request.getAttribute("allNodeList");
                int deep = 1;
                for (ResourceNode node : nodeList) {
                    out.print(loopTree(node, deep, node.getId()));
                }
            %>
                <tr>
                    <td>
                        <input type="button" class="button button-blue" value="更新用户组权限绑定" onclick="Qadmin.App.Resource.bindGroupResource()"/>
                        <input type="button" class="button button-blue" value="刷新" onclick="location.reload();"/>
                    </td>
                </tr>
        </table>
        </div>
    </div>
<script type="text/javascript">
var bindRidList = ${ridList}; // binded resource id list
$(document).ready(function() {
    for(var i =0; i<bindRidList.length; i++) {
        $('#node_'+ bindRidList[i]).attr("checked", true);
    }
});
function selChildren(e, children) {
    if(children!="") {
	var arr = children.slice(0, children.length-1).split(",");
	for(var i =0; i<arr.length; i++) {
	    $('#node_'+arr[i]).attr("checked", e.checked);
	}
    }
}
</script>
</body>
</html>
